<template>
    <div>
        <a-modal
         v-model:visible="visible" 
         :title="formState.id?(formState.view?'查看':'编辑'):'新增'" 
         @ok="handleOk" 
         @cancel="visible = false"
         class="enterprise-add-edit-modal"
         :width="700"
         :footer="null"
        >
            <div v-if="visible">
                <a-form :form="form" layout="horizontal" class="add-edit-form">
                    <a-form-item label="通用名称">
                        <a-input
                        v-model:value="formState.ftymc"
                        placeholder="请输入通用名称"
                        v-decorator="[
                            'ftymc', { initialValue:formState.ftymc, rules: rules.ftymc}
                        ]"/>
                    </a-form-item>
                    <a-form-item label="商品名">
                        <a-input v-model:value="formState.fspm" placeholder="请输入商品名"/>
                    </a-form-item>
                    <a-form-item label="执行标准">
                        <a-input v-model:value="formState.fzxbz" placeholder="请输入执行标准"/>
                    </a-form-item>
                    <a-form-item label="药品有效期">
                        <a-date-picker v-model:value="formState.fypyxq" placeholder="请选择药品有效期"/>
                    </a-form-item>

                    <a-form-item label="药品分类">
                        <a-input v-model:value="formState.fcplb" placeholder="请输入药品分类"/>
                    </a-form-item>
                    <a-form-item label="规格" class="flex-1-box">
                        <a-textarea 
                            placeholder="请输入规格"
                            v-model:value="formState.fgg"
                            :autosize="{ minRows: 1, maxRows: 10000 }"
                        ></a-textarea>
                    </a-form-item>
                    <a-form-item label="剂型">
                        <a-input v-model:value="formState.fjx" placeholder="请输入剂型"/>
                    </a-form-item>
                    <a-form-item label="处方" class="flex-1-box">
                        <a-textarea 
                            placeholder="请输入处方"
                            v-model:value="formState.fcf"
                            :autosize="{ minRows: 1, maxRows: 10000 }"
                        ></a-textarea>
                    </a-form-item>
                    <a-form-item label="批准文号" class="flex-1-box">
                        <a-input v-model:value="formState.fpzwh" placeholder="请输入批准文号"/>
                    </a-form-item>
                    <a-form-item label="批准文号有限期" class="flex-1-box">
                        <a-date-picker v-model:value="formState.fpzwhyxq" placeholder="请选择批准文号有限期"/>
                    </a-form-item>
                    
                </a-form>
            </div>
            <div style="
                display: flex; justify-content: flex-end;
                margin-top: 20px; width: 100%;
                background-color: #fff;">
                <a-button @click="visible = false">取消</a-button>
                <a-button v-if="!formState.view" style="margin: 0 20px;" class="blue-btn" type="primary" @click="handleOk(true)">保存</a-button>
                <a-button v-if="!formState.view" class="blue-btn" type="primary" @click="handleOk(false)">保存并关闭</a-button>
            </div>
        </a-modal>
    </div>
</template>
<script>
import compSelector from '@/components/compSelector/compSelector.vue'
import moment from 'moment'
export default {
    components:{
        compSelector,
    },
    props:{

    },
    data(){
        return{
            visible:false,
            formState:{},
            form: this.$form.createForm(this, { name: 'coordinated' }),
            rules:{
                ftymc: [{
                    required: true,
                    message: '请输入偏差主题',
                    trigger: 'blur',
                }],
            },
        }
    },
    watch:{

    },
    mounted(){
    },
    methods:{
        getData(val){
            this.formState = {}
            this.formState = {...val,fpzwhyxq:val.fpzwhyxq?moment(val.fpzwhyxq,'YYYY-MM-DD'):'',fypyxq:val.fypyxq?moment(val.fypyxq,'YYYY-MM-DD'):''}
        },
        handleOk(val){
            this.form.validateFields((err, values) => {
                if (!err) {
                    if(this.formState.id){
                        this.$emit('editItem',{
                            ...this.formState,
                            ...values,
                            fpzwhyxq:this.formState.fpzwhyxq?moment(this.formState.fpzwhyxq).format('YYYY-MM-DD'):'',
                            fypyxq:this.formState.fypyxq?moment(this.formState.fypyxq).format('YYYY-MM-DD'):'',
                        },val)
                    }else{
                        this.$emit('addItem',{
                            ...this.formState,
                            ...values,
                            fpzwhyxq:this.formState.fpzwhyxq?moment(this.formState.fpzwhyxq).format('YYYY-MM-DD'):'',
                            fypyxq:this.formState.fypyxq?moment(this.formState.fypyxq).format('YYYY-MM-DD'):'',
                        },val)
                    }
                    
                }
            })
        },
    }
}
</script>
<style lang="less">
.enterprise-add-edit-modal{
    .add-edit-form{
        display: flex;
        flex-wrap: wrap;
        .ant-form-item{
            display: flex;
        }
        .ant-form-item-label{
            width: 85px;
        }
        .ant-form-item-control-wrapper{
            width: 180px;
        }
        .flex-1-box{
            width: 100%;
            .ant-form-item-control-wrapper{
                flex: 1;
            }
        }
    }
}
</style>